package nero.yncc.com.createart.activity;

import android.support.v7.widget.RecyclerView;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.adapter.AdapterFoundArticle;
import nero.yncc.com.createart.adapter.AdapterIndexHotSale;
import nero.yncc.com.createart.adapter.AdapterIndexNewGod;
import nero.yncc.com.createart.adapter.AdapterIndexTejia;
import nero.yncc.com.createart.base.BaseActivity;
import nero.yncc.com.createart.base.Constants;
import nero.yncc.com.createart.bean.BeanArticle;
import nero.yncc.com.createart.bean.GodBean;
import nero.yncc.com.createart.bean.TejiaBean;
import nero.yncc.com.createart.utils.HttpUtils;

/**
 * Created by nero on 17-6-30.
 */

@ContentView(R.layout.single_pulltorefresh)
public class ActivityMore extends BaseActivity {
    @ViewInject(R.id.pl_list)
    PullToRefreshListView pl_list;


    String type = "";  //发现 新品 热销 特价 discover, xinpin, rexiao,tejia

    AdapterFoundArticle adapterFoundArticle;
    AdapterIndexHotSale adapterIndexNewGod;
    AdapterIndexHotSale adapterIndexHotSale;
    AdapterIndexTejia adapterIndexTejia;

    ArrayList<BeanArticle> arrayList_found;
    ArrayList<GodBean.Goods> arrayList_new;
    ArrayList<GodBean.Goods> arrayList_hot;
    ArrayList<TejiaBean> arrayList_tejia;


    int pageSize=10, page=1;
    boolean canLoad;

    @Override
    protected void initData() {
        super.initData();
        switch (type) {
            case "发现":
//                arrayList_found = new ArrayList<>();
//                adapterFoundArticle = new AdapterFoundArticle(mContext,null, arrayList_found);
//                pl_list.setAdapter(adapterFoundArticle);
                break;
            case "新品":
                arrayList_new = new ArrayList<>();
                adapterIndexNewGod = new AdapterIndexHotSale(mContext, arrayList_new);
                pl_list.setAdapter(adapterIndexNewGod);
                break;
            case "本周热销":
                arrayList_hot = new ArrayList<>();
                adapterIndexHotSale = new AdapterIndexHotSale(mContext, arrayList_hot);
                pl_list.setAdapter(adapterIndexHotSale);
                break;
            case "特价":
                arrayList_tejia = new ArrayList<>();
                adapterIndexTejia = new AdapterIndexTejia(mContext, arrayList_tejia);
                pl_list.setAdapter(adapterIndexTejia);
                break;
        }


    }

    @Override
    protected void initListener() {
        super.initListener();
        pl_list.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 1;
                canLoad = true;
                switch (type) {
                    case "发现":
                        arrayList_found .clear();
                        adapterFoundArticle.notifyDataSetChanged();
                        break;
                    case "新品":
                        arrayList_new .clear();
                        adapterIndexNewGod.notifyDataSetChanged();
                        break;
                    case "本周热销":
                        arrayList_hot.clear();
                        adapterIndexHotSale .notifyDataSetChanged();
                        break;
                    case "特价":
                        arrayList_tejia .clear();
                        adapterIndexTejia .notifyDataSetChanged();
                        break;
                }
                getData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                getData();
            }
        });
        pl_list.setRefreshing(true);
    }

    @Override
    protected void initView() {
        super.initView();
        type = getIntent().getStringExtra("type");

        setPullToRefreshListView(pl_list);
        setTitleBarTitle(type);
    }

    @Override
    protected void getData() {
        super.getData();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("pagesize", pageSize + "");
        hashMap.put("pageindex", page + "");
        switch (type) {
            case "发现":
                hashMap.put("type", "discover");
                break;
            case "新品":
                hashMap.put("type", "xinpin");
                break;
            case "本周热销":
                hashMap.put("type", "rexiao");
                break;
            case "特价":
                hashMap.put("type", "tejia");
                break;
        }
//        "count":149,
//                "list":[
        HttpUtils.get(this, Constants.API_MORE, hashMap, null, HttpUtils.TYPE_NOTHING, new HttpUtils.HttpCallback() {
            @Override
            public void success(String msg, Object json) {
                JSONArray jsonArray = null;

                try {
                    jsonArray =new JSONObject(json.toString()).getJSONArray("list");
                    if (!canLoad) {
                        showToast("没有更多内容了");
                        pl_list.onRefreshComplete();
                        return;
                    }
                    if (jsonArray.length() < pageSize) {
                        canLoad = false;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    showToast("获取列表失败，请再次尝试");
                    return;
                }


                for (int a = 0; a < jsonArray.length(); a++) {
                    try {
                        switch (type) {
                            case "发现":
                                arrayList_found.add(JSON.parseObject(jsonArray.getString(a), BeanArticle.class));
                                break;
                            case "新品":
                                arrayList_new.add(JSON.parseObject(jsonArray.getString(a), GodBean.Goods.class));
                                break;
                            case "本周热销":
                                arrayList_hot.add(JSON.parseObject(jsonArray.getString(a), GodBean.Goods.class));
                                break;
                            case "特价":
                                arrayList_tejia.add(JSON.parseObject(jsonArray.getString(a), TejiaBean.class));
                                break;
                        }
                    } catch (Exception e) {

                    }
                }

                switch (type) {
                    case "发现":
                        adapterFoundArticle.notifyDataSetChanged();
                        break;
                    case "新品":
                        adapterIndexNewGod.notifyDataSetChanged();
                        break;
                    case "本周热销":
                        adapterIndexHotSale.notifyDataSetChanged();
                        break;
                    case "特价":
                        adapterIndexTejia.notifyDataSetChanged();
                        break;
                }
                pl_list.onRefreshComplete();
            }

            @Override
            public void fail(String failMsg) {
                showToast(failMsg);
                pl_list.onRefreshComplete();
            }
        });

    }
}
